package com.lzw.breezeNovel.webApp.service.impl.video;

import com.lzw.breezeNovel.model.entity.pojo.video.Movie;
import com.lzw.breezeNovel.model.entity.query.PaginationResultQuery;
import com.lzw.breezeNovel.webApp.mapper.video.MovieMapper;
import com.lzw.breezeNovel.webApp.service.video.MovieService;
import com.lzw.breezeNovel.webApp.vo.video.MovieVo;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.service.impl.video
 * @date 2025/3/18
 * @description 电影
 */
@Slf4j
@Service
@SuppressWarnings("all")
public class MovieServiceImpl implements MovieService {

    /**
     * 注入mapper
     */
    @Autowired
    private MovieMapper movieMapper;

    /**
     * 获取电影列表
     * @param page
     * @param size
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public PaginationResultQuery<MovieVo> getMovie(int page, int size) {
        //计算分页
        int startPage = startPage(page, size);
        //查询数据库获取总数居
        Long count = movieMapper.selectCount();
        //判断是否有数据
        if (count == null || count == 0) {
            return new PaginationResultQuery<>(0, size, page, 0, Collections.emptyList());
        }
        //获取数据
        List<Movie> movieVos = movieMapper.selectListByPage(startPage, size);
        //进行拷贝
        List<MovieVo> movieVoList = Copy(movieVos);
        //返回结果
        return new PaginationResultQuery<>(Math.toIntExact(count), size, page, movieVoList);
    }

    /**
     * 根据类型获取电影
     *
     * @param type
     * @param videoId
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public List<MovieVo> getMovieByType(String type, Integer videoId) {
        if(type == null || type.isEmpty()){
            return List.of();
        }
        //查询数据库获取数据
        List<Movie> movieVos = movieMapper.selectListByType(type, videoId);
        //进行拷贝
        return Copy(movieVos);
    }

    /**
     * 拷贝
     * @param movieVos
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private List<MovieVo> Copy(List<Movie> movieVos) {
        //判断数是否存在
        if (movieVos == null || movieVos.size() == 0) {
            return null;
        }
        return movieVos.stream().map(movie -> {
            MovieVo movieVo = new MovieVo();
            BeanUtils.copyProperties(movie, movieVo);
            return movieVo;
        }).collect(Collectors.toList());
    }

    /**
     * 计算分页
     * @param pageNo
     * @param size
     * @return
     */
    public int startPage(Integer pageNo, Integer size){
        //判断分页数据是否为空
        pageNo = Optional.of(pageNo).orElse(1);
        size = Optional.of(size).orElse(10);
        pageNo = Math.max(pageNo, 1);
        size = Math.max(size, 1);
        //计算分页
        pageNo = (pageNo - 1) * size;
        return pageNo;
    }
}
